package cn.e4j.modular.crm.controller;


import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.modular.crm.entity.GwgaT;
import cn.e4j.modular.crm.entity.GwgbT;
import cn.e4j.modular.crm.service.Gwgi001Service;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/crm/gwgi001")
public class Gwgi001Controller extends BaseController {

    @Autowired
    Gwgi001Service gwgi001Service;

    /**
     * 跳转至招标信息维护主界面
     *
     * @return
     */
    @RequiresPermissions("crm:gwgi001:init")
    @RequestMapping(value = {"", "/init"})
    public String index() {
        return "/pc/crm/gwgi001.html";
    }

    @RequiresPermissions("crm:gwgi001:init")
    @RequestMapping("/getHeadList")
    @ResponseBody
    public ResponseData getHeadList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Page<Object> page = dtoWapper.getPage();
        List<Map<String, Object>> list = gwgi001Service.getHeadList(page, inDto);
        return new PageReData(page, list);
    }

    @RequiresPermissions("crm:gwgi001:init")
    @RequestMapping("/getGwgbList")
    @ResponseBody
    public ResponseData getGwgbList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Page<Object> page = dtoWapper.getPage();
        List<Map<String, Object>> list = gwgi001Service.getGwgbList(page, inDto);
        return new PageReData(page, list);
    }

    @RequiresPermissions("crm:gwgi001:init")
    @RequestMapping("/getGwgcList")
    @ResponseBody
    public ResponseData getGwgcList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Page<Object> page = dtoWapper.getPage();
        List<Map<String, Object>> list = gwgi001Service.getGwgcList(page, inDto);
        return new PageReData(page, list);
    }

    @RequiresPermissions("crm:gwgi001:init")
    @RequestMapping("/getDetailList")
    @ResponseBody
    public ResponseData getDetailList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwgbT> list = gwgi001Service.getDetailList(inDto);
        return new SuccessResponseData(list);
    }

    @RequiresPermissions("crm:gwgi001:init")
    @RequestMapping("/getGwgaDetailList")
    @ResponseBody
    public ResponseData getGwgaDetailList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwgaT> list = gwgi001Service.getGwgaDetailList(inDto);
        return new SuccessResponseData(list);
    }

    @RequiresPermissions("crm:gwgi001:add")
    @RequestMapping("/add")
    @ResponseBody
    public ResponseData add(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<GwgaT> gwgaTList = JSONObject.parseArray(jsonObject.getString("gwgaTList"), GwgaT.class);
        List<GwgbT> gwgbTList = JSONObject.parseArray(jsonObject.getString("gwgbTList"), GwgbT.class);
        if (gwgi001Service.existedTheParams(gwgaTList.get(0))) {
            return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                    BizExceptionEnum.EXISTED_THE_PARAMS.getMessage());
        } else {
            gwgi001Service.add(gwgaTList,gwgbTList);
            return SUCCESS_TIP;
        }
    }

    @RequiresPermissions("crm:gwgi001:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public ResponseData edit(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<GwgaT> gwgaTList = JSONObject.parseArray(jsonObject.getString("gwgaTList"), GwgaT.class);
        List<GwgbT> gwgbTList = JSONObject.parseArray(jsonObject.getString("gwgbTList"), GwgbT.class);
        gwgi001Service.edit(gwgaTList,gwgbTList);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("crm:gwgi001:del")
    @RequestMapping("/del")
    @ResponseBody
    public ResponseData delRow(GwgaT gwgaT) {
        // 判断对应的商机主题是否在拜访记录中被使用
        if(gwgi001Service.existedGwbh(gwgaT)) {
            return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                    "招标对应的商机主题已被拜访记录使用，不可删除");
        }
        // 判断招标编号在投标中是否被使用
        if(gwgi001Service.existedGwgc(gwgaT)){
            return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                    "该招标编号已被投标申请使用，不可删除");
        }
        gwgi001Service.delete(gwgaT);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("crm:gwgi001:del")
    @RequestMapping("/delBatch")
    @ResponseBody
    public ResponseData delRowsBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwgaT> gwgaTList = inDto.getReqList(GwgaT.class);
        for (int i = 0; i < gwgaTList.size(); i++) {
            GwgaT gwgaT = gwgaTList.get(i);
            // 判断对应的商机主题是否在拜访记录中被使用
            if(gwgi001Service.existedGwbh(gwgaT)) {
                return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                        "招标对应的商机主题已被拜访记录使用，不可删除");
            }
            // 判断招标编号在投标中是否被使用
            if(gwgi001Service.existedGwgc(gwgaT)){
                return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                        "该招标编号已被投标申请使用，不可删除");
            }
        }
        gwgi001Service.deleteBatch(gwgaTList);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("crm:gwgi001:add")
    @RequestMapping("/getAddData")
    @ResponseBody
    public ResponseData getAddData(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        outDto.put("CURRENT_USER_ID",shiroUser.getID());
        outDto.put("CURRENT_USER_NAME",shiroUser.getNAME());
        outDto.put("CURRENT_ORG",shiroUser.getORG_ID());
        outDto.put("CURRENT_ORG_NAME",shiroUser.getSysOrgT().getNAME());
        outDto.put("CURRENT_DATE", LocalDate.now());
        return new SuccessResponseData(outDto);
    }

    @RequiresPermissions("crm:gwgi001:edit")
    @RequestMapping("/changeStatus")
    @ResponseBody
    public ResponseData changeStatus(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwgaT> gwgaTList = inDto.getReqList(GwgaT.class);
        gwgi001Service.changeStatusBatch(gwgaTList);
        return SUCCESS_TIP;
    }

    @RequestMapping("/getGwgaData")
    @ResponseBody
    public ResponseData getGwgaData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Page<Object> page = dtoWapper.getPage();
        List<Map<String, Object>> list = gwgi001Service.getGwgaData(page, inDto);
        return new PageReData(page, list);
    }
}
